//
//  OPMrestDetailController.m
//  OnePiece
//
//  Created by wsy on 15/10/29.
//  Copyright © 2015年 wsy. All rights reserved.
//

#import "OPMrestDetailController.h"
#import "OPMrestCookCell.h"
#import "OPMrestCookHeadView.h"
#import "OPRequesterManager.h"
#import "UMSocial.h"
#import <UIImageView+WebCache.h>
@interface OPMrestDetailController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) NSMutableArray *dataSourceArr;
@property (nonatomic, strong) UIImageView *shareimg;
@property (nonatomic, strong) OPMrestInfoModel *infoModel;
@end

@implementation OPMrestDetailController
- (NSMutableArray *)dataSourceArr{
    if (_dataSourceArr == nil) {
        _dataSourceArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArr;
}
- (void)configureData
{
    NSString *str1 = @"http://www.youyuwei.com/api/dish?app_code=com.youyuwei.yuwei.ios&hardware_model=%2C2&app_ver=2.1&sys_ver=&device_type=ios&ywsdk_ver=20131018&open_udid=&start=0&oauth_timestamp=1446084104&list=listofRest&device_token=&oauth_signature_method=HMAC-SHA1&x_auth_mode=client_auth&restid=";
    NSString *str2 = @"&ver=2&idfa=&channel_id=App%20Store&oauth_token=&oauth_consumer_key=4&oauth_version=1.0&oauth_signature=&debug=0&oauth_nonce=";
    NSString *urlstr = [NSString stringWithFormat:@"%@%@%@",str1, self.restid, str2];
    [OPRequesterManager requestWithType:GET urlString:urlstr parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        _infoModel = [[OPMrestInfoModel alloc]init];
        [_infoModel setValuesForKeysWithDictionary:dic[@"data"][@"restinfo"]];
        OPMrestCookHeadView *headView = [[OPMrestCookHeadView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, [OPMrestCookHeadView heightForHead:_infoModel])];
        [headView setModel:_infoModel];
        _mainTableView.tableHeaderView = headView;
        self.titleLable.text = _infoModel.name;
        _shareimg = [[UIImageView alloc]init];
        [_shareimg sd_setImageWithURL:[NSURL URLWithString:_infoModel.imgs[0]]];
        
        NSArray *array = dic[@"data"][@"list"];
        NSLog(@"%@",array);
        for (NSDictionary *dict in array) {
            OPMrestCookModel *m = [[OPMrestCookModel alloc]init];
            [m setValuesForKeysWithDictionary:dict];
            [self.dataSourceArr addObject:m];
        }
        [_mainTableView reloadData];
    } error:^(NSError *error) {
        NSLog(@"%@",[error localizedDescription]);
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.leftButton setImage:[UIImage imageNamed:@"meishi-back"] forState:(UIControlStateNormal)];
    [self.leftButton addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];
    // Do any additional setup after loading the view.
    _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    _mainTableView.dataSource = self;
    _mainTableView.delegate = self;
    _mainTableView.showsVerticalScrollIndicator = NO;
    _mainTableView.rowHeight = SCREEN_WIDTH*235/600+50;
    [self.view addSubview:_mainTableView];
    [_mainTableView registerClass:[OPMrestCookCell class] forCellReuseIdentifier:@"cell"];
    [self configureData];

    UIButton *shareButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    shareButton.frame = CGRectMake(SCREEN_WIDTH - 45, 15, 20, 20);
    [shareButton setImage:[UIImage imageNamed:@"meishi-share"] forState:(UIControlStateNormal)];
    [shareButton addTarget:self action:@selector(share) forControlEvents:(UIControlEventTouchUpInside)];
    [self.topView addSubview:shareButton];
    
}
- (void)share
{
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"562dcfa4e0f55a2a14004075"
                                      shareText:_infoModel.keyword
                                     shareImage:_shareimg.image
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToRenren,UMShareToDouban,UMShareToFacebook,UMShareToTwitter,nil]
                                       delegate:nil];
}
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectio
{
    return _dataSourceArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OPMrestCookCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    OPMrestCookModel *m = _dataSourceArr[indexPath.row];
    [cell setModel:m];
    return cell;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
